package zut.cs.liziflower.controller;


import org.springframework.web.bind.annotation.*;
import zut.cs.liziflower.entity.Order;
import zut.cs.liziflower.entity.Result;
import zut.cs.liziflower.mapper.OrderMapper;
import zut.cs.liziflower.service.impl.ConsumerServiceImpl;
import zut.cs.liziflower.service.impl.OrderServiceImpl;
import zut.cs.liziflower.service.impl.OrderitemsServiceImpl;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xuwenke
 * @since 2022-05-26
 */
@RestController
@RequestMapping("/api/order")
public class OrderController {

    @Resource
    private OrderServiceImpl orderService;
    @Resource
    private ConsumerServiceImpl consumerService;
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private OrderitemsServiceImpl orderitemsService;

    @PostMapping("add-order")
    public Result<?> addOrder(@RequestBody Order order) {
        //添加，如果添加成功，返回对应的orderid
        int i = orderService.addOrder(order);
        if (i == 1) { //添加成功
            Order o = orderService.getOrder(order);
            if (o != null) {
                return Result.success(o.getOrderid());
            } else {
                return Result.error("405", "失败", null);
            }
        }
        return Result.error("405", "失败", null);
    }

    @GetMapping("/get-all-paid-orders")
    public Result<?> getAllPaidOrders(@RequestParam("userid") Integer userid,
                                      @RequestParam("status") Integer status) {
        //待支付的订单
        List<Order> list = orderService.getAllPaidOrders(userid, status);
//        if (list.isEmpty()) {
//            return Result.error("405", "错误", null);
//        }
        return Result.success(list);
    }

    @PutMapping("/update-paidto-pending")
    public Result<?> updatePaidtoPending(@RequestBody Order order) {
        //完成订单支付，----> 待收货
        //status 更新为 40;余额money更新为money-totalprice ,积分更新为 score + totalprice

        //更新order.status = 40
//        UpdateWrapper<Order> updateWrapper = new UpdateWrapper<>();
//        updateWrapper.eq("orderid", order.getOrderid()).set("status", 40);
        int flag;
        flag = orderService.updateStatus(order);
//        System.out.println("i: "+i);
        if (flag == 1) {//修改成功
            //更新用户的money和score
            flag = consumerService.updateMoneyAndScore(order.getUserid(),
                    (order.getMoney().subtract(order.getTotalprice())),
                    order.getScore() + order.getTotalprice().intValue());
            if (flag == 1) {
                //成功
                return Result.success("支付成功");
            }
        }
        return Result.error("405", "支付失败", null);
    }

    @PutMapping("/updated-finished")
    public Result<?> updatedFinished(@RequestBody Order order) {
        //完成订单
        int flag = orderService.updatedFinished(order.getOrderid(), order.getUserid(), order.getStatus());
        if (flag == 1) {
            return Result.success("收货成功");
        }
        return Result.error("405", "收货失败", null);
    }

    @GetMapping("/get-all-items-pending")
    public Result<?> getAllItemsPending(@RequestParam("status") Integer status,
                                        @RequestParam("userid") Integer userid) {
        //获取所有待收货的订单
        List<Order> list = orderService.getAllPaidOrders(userid, status);
        return Result.success(list);
    }

    @GetMapping("/get-all-items-finished")
    public Result<?> getAllItemsFinished(@RequestParam("status") Integer status,
                                         @RequestParam("userid") Integer userid) {
        //获取所有已完成的订单
        List<Order> list = orderService.getAllPaidOrders(userid, status);
        return Result.success(list);
    }

    /**
     * 删除待支付订单
     * 同时删除子订单
     *
     * @param orderid
     * @param userid
     * @return
     */
    @DeleteMapping("/delete-items-paid")
    public Result<?> deletePaid(@RequestParam("orderid") Integer orderid,
                                @RequestParam("userid") Integer userid) {

        //删除order中的订单信息
        int del = orderService.deletePaid(orderid, userid);

        //删除orderitems中的信息
        if (del == 1) {
            int d = orderitemsService.deletePaid(orderid, userid);
            if (d == 1) {
                return Result.success("删除成功");
            }
        }
        return Result.error("405", "删除失败", null);
    }

}
